package com.situ.hospital.service.impl;

import com.situ.hospital.dao.IMedicineDAO;
import com.situ.hospital.dao.Impl.MedicineDAOImpl;
import com.situ.hospital.listener.MedicineExcelListener;
import com.situ.hospital.pojo.Medicine;
import com.situ.hospital.pojo.query.MedicineQuery;
import com.situ.hospital.pojo.vo.InventoryVO;
import com.situ.hospital.pojo.vo.MedicineVO;
import com.situ.hospital.service.IMedicineService;
import com.situ.hospital.util.ExcelUtils;
import com.situ.hospital.util.PageResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

public class MedicineServiceImpl implements IMedicineService {

    private IMedicineDAO medicineDAO = new MedicineDAOImpl();

    @Override
    public PageResult<MedicineVO> selectByPage(MedicineQuery medicineQuery) {
        List<MedicineVO> list = medicineDAO.selectByPage(medicineQuery);
        int totalCount = medicineDAO.selectTotalCount(medicineQuery);
        return new PageResult<>(0, "", totalCount, list);
    }

    @Override
    public void deleteById(int id) {
        medicineDAO.deleteById(id);
    }

    @Override
    public void deleteAll(String[] ids) {
        for(String id : ids) {
            medicineDAO.deleteById(Integer.parseInt(id));
        }
    }

    @Override
    public void add(Medicine medicine) {
        medicineDAO.add(medicine);
    }

    @Override
    public Medicine selectById(int id) {
        return medicineDAO.selectById(id);
    }

    @Override
    public void update(Medicine medicine) {
        medicineDAO.update(medicine);
    }

    @Override
    public void exportExcel(HttpServletResponse resp) {
        List<Medicine> list = medicineDAO.selectAll();
        ExcelUtils.exportExcel(resp, list, Medicine.class, "medicine");
    }

    @Override
    public void importExcel(HttpServletRequest req) {
        ExcelUtils.importExcel(req,new MedicineExcelListener(),Medicine.class);
    }

    @Override
    public List<Medicine> selectAll() {
        return medicineDAO.selectAll();
    }

    @Override
    public List<InventoryVO> selectQuantity(int id) {
        List<InventoryVO> list = new ArrayList<>();
        Medicine medicine = medicineDAO.selectById(id);
        for (int i = 1; i <= medicine.getInventory(); i++) {
            InventoryVO quantity = new InventoryVO();
            quantity.setQuantity(i);
            list.add(quantity);
        }
        return list;
    }
}
